package cn.fastoo.common.dao.system;

import cn.fastoo.common.entity.system.Permission;
import cn.fastoo.common.entity.system.Role;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RoleDao extends FastDaoImpl<Role> {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 获取用户可分配角色page数据，因为前端交互的一些问题，暂时不提供搜索功能
     *
     * @param pageRequest
     * @return
     */
    public Page<Role> getUserChooseRolePage(PageRequest pageRequest) {
        return findPage(new Search().addPageRequest(pageRequest).addSearchFilter("NOTIN_id","1"));
    }

    /**
     * 递归获取角色所包含的全部权限
     *
     * @param role
     * @return
     */
    public List<Permission> getPermissionByRecursively(Role role) {
        List<Permission> set = new ArrayList<Permission>();
        set.addAll(permissionDao.getRolePermissions(role));
        if (role.getRoleList().isEmpty()) {
            return set;
        } else {
            List<Role> roleList = role.getRoleList();
            for (Role cRole : roleList) {
                List<Permission> pList = getPermissionByRecursively(cRole);
                set.addAll(pList);
            }
            return set;
        }
    }


}
